/**
 * @Title:  AttentionDynamicsActivity.java 
 * @Copyright:  XXX Co., Ltd. Copyright YYYY-YYYY,  All rights reserved 
 * @Description:  TODO<请描述此文件是做什么的> 
 * @author:  MeiYi 
 * @data:  2015-12-7 上午10:20:14 
 * @version:  V1.0 
 */

package artid.com.arttogoa.fragment;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.InputType;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import artid.com.arttogoa.R;
import artid.com.arttogoa.db.DataKeeper;
import artid.com.arttogoa.glide.GlideUtil;
import artid.com.arttogoa.parser.ArtworkImageUpload;
import artid.com.arttogoa.parser.MicMapBean;
import artid.com.arttogoa.parser.RecordDetailList;
import artid.com.arttogoa.task.CallbackSky;
import artid.com.arttogoa.util.AlumAlert;
import artid.com.arttogoa.util.VioceUtils;

/**
 * TODO<请描述这个类是干什么的>
 * 
 * @author 熊彬
 * @data: 2015-12-7 上午10:20:14
 * @version: V1.0
 */
public class CompleteGraphFragment extends Fragment implements OnClickListener {
	private TextView mTextView, option;
	private ImageView mImageView, Inscribe, vioce;
	private MicMapBean data;
	private CallbackSky call;

	private RecordDetailList item;
	private EditText info, title, realname, spce;
	private TextView age;
	private String infoString, titleString, realnameString, ageString,
			spceString, typeString;
	private EditText width, height;// 尺寸

	// public boolean chenge = false;

	public CompleteGraphFragment(CallbackSky call) {
		this.call = call;
	}

	private VioceUtils vu;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater,
	 * android.view.ViewGroup, android.os.Bundle)
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View v = inflater.inflate(R.layout.fragment_complete_graph, null);
		mTextView = (TextView) v.findViewById(R.id.tv_edit);// 图片编辑按钮
		option = (TextView) v.findViewById(R.id.tv_option);// 分类
		mImageView = (ImageView) v.findViewById(R.id.iv_complete_image);// 完整图
		Inscribe = (ImageView) v.findViewById(R.id.iv_Inscribe_image);// 落款
		info = (EditText) v.findViewById(R.id.et_info);// 完整图介绍
		title = (EditText) v.findViewById(R.id.et_title);// 作品名称
		realname = (EditText) v.findViewById(R.id.et_realname);// 作者
		age = (TextView) v.findViewById(R.id.et_age);// 年代
		age.setOnClickListener(this);

		width = (EditText) v.findViewById(R.id.et_width);
		height = (EditText) v.findViewById(R.id.et_height);
		width.setInputType(InputType.TYPE_CLASS_NUMBER
				| InputType.TYPE_NUMBER_FLAG_DECIMAL);
		height.setInputType(InputType.TYPE_CLASS_NUMBER
				| InputType.TYPE_NUMBER_FLAG_DECIMAL);

		vioce = (ImageView) v.findViewById(R.id.iv_vioce);
		vu = new VioceUtils(getActivity(), vioce, info);
		vu.init();
		mTextView.setOnClickListener(this);
		Inscribe.setOnClickListener(this);
		item = (RecordDetailList) DataKeeper.readSerializable(getActivity(),
				RecordDetailList.class);
		setCompleteImage(item.getPointinformation().get(0).getImgurl(), false);
		setInscribeImage(item.getInscribeimgurl());
		infoString = item.getDetail().trim();
		typeString = item.getArt_type().trim();
		titleString = item.getTitle().trim();

		realnameString = item.getRealname();
		ageString = item.getAge().trim();

		// spceS tring = item.getWidth() + " X " + item.getHeight();

		title.setText(TextUtils.isEmpty(titleString) ? "" : titleString);
		realname.setText(TextUtils.isEmpty(realnameString) ? ""
				: realnameString);
		width.setText(TextUtils.isEmpty(item.getWidth()) ? ""
				: formatConversion(item.getWidth()));
		height.setText(TextUtils.isEmpty(item.getHeight()) ? ""
				: formatConversion(item.getHeight()));

		age.setText(TextUtils.isEmpty(ageString) ? "" : ageString);
		// spce.setText(TextUtils.isEmpty(spceString) ? "" : spceString);
		info.setText(infoString);
		// initScrollBar();
		option.setText(typeString);
		// adapter = new AuctionAdapter(getActivity());
		// data = (MicMapBean) getArguments().getSerializable("data");
		// mImageView.setImageBitmap(getLoacalBitmap(data.getImage()));

		return v;
	}

	// private List<Classlist> class_list;

	// private void getTypeId(String typeString) {
	// for (int i = 0; i < class_list.size(); i++) {
	// if (class_list.get(i).getName().equals(typeString)) {
	// typeId = class_list.get(i).getId();
	// }
	// }
	// }

	public ArtworkImageUpload getData() {

		if (!info.getText().toString().trim().equals(infoString))
			EditCache.cChenge = true;
		else if (!title.getText().toString().trim().equals(titleString))
			EditCache.cChenge = true;
		else if (!realname.getText().toString().trim().equals(realnameString))
			EditCache.cChenge = true;
		else if (!width
				.getText()
				.toString()
				.trim()
				.equals(TextUtils.isEmpty(item.getWidth()) ? ""
						: formatConversion(item.getWidth())))
			EditCache.cChenge = true;
		else if (!height
				.getText()
				.toString()
				.trim()
				.equals(TextUtils.isEmpty(item.getHeight()) ? ""
						: formatConversion(item.getHeight())))
			EditCache.cChenge = true;
		else if (!age.getText().toString().trim().equals(ageString))
			EditCache.cChenge = true;
		else if (!option.getText().toString().trim().equals(typeString))
			EditCache.cChenge = true;
		System.out.println(height.getText().toString().trim() + "=="
				+ (TextUtils.isEmpty(item.getHeight()) ? ""
				: formatConversion(item.getHeight())));
		ArtworkImageUpload artwork = new ArtworkImageUpload();
		String titleString = title.getText().toString();
		String infoString = info.getText().toString();
		String realnameString = realname.getText().toString();
		String ageString = age.getText().toString();
		String widthString = width.getText().toString();
		String heightString = height.getText().toString();
		if (TextUtils.isEmpty(titleString)) {
			Toast.makeText(getActivity(), "作品名称不能为空~", Toast.LENGTH_SHORT)
					.show();
			return null;
		} else if (TextUtils.isEmpty(realnameString)) {
			Toast.makeText(getActivity(), "作者名不能为空~", Toast.LENGTH_SHORT)
					.show();
			return null;
		} else if (TextUtils.isEmpty(ageString)) {
			Toast.makeText(getActivity(), "年代不能为空~", Toast.LENGTH_SHORT).show();
			return null;
		} else if (TextUtils.isEmpty(widthString)
				|| TextUtils.isEmpty(heightString)) {
			Toast.makeText(getActivity(), "尺寸不能为空~", Toast.LENGTH_SHORT).show();
			return null;
		} else if (TextUtils.isEmpty(infoString)) {
			Toast.makeText(getActivity(), "描述不能为空~", Toast.LENGTH_SHORT).show();
			return null;
		}

		artwork.setChenge(true);
		artwork.setWidth(widthString);
		artwork.setHeight(heightString);
		artwork.setTitle(titleString);
		artwork.setArt_type("1");
		artwork.setRealname(realnameString);
		artwork.setAge(ageString);
		artwork.setDetail(infoString);
		return artwork;
	}

	/**
	 * 
	 * @Title: formatConversion
	 * @Description: TODO 得到纯净的尺寸
	 * @param @param str
	 * @param @return 设定文件
	 * @return String 返回类型
	 * @throws
	 */
	public static String formatConversion(String str) {
		str = str.trim();
		if (str.contains("cm")) {
			str = str.substring(0, str.length() - 2);
			if (str.endsWith("0")) {
				str = str.substring(0, str.length() - 1);
				if (str.endsWith("0")) {
					str = str.substring(0, str.length() - 1);
					if (str.endsWith(".")) {
						str = str.substring(0, str.length() - 1);
						return str.trim();
					}
					return str.trim();
				}
				return str.trim();
			}
		} else {
			return str.trim();
		}
		return str.trim();

	}

	/**
	 * 加载本地图片 http://bbs.3gstdy.com
	 * 
	 * @param url
	 * @return
	 */
	public Bitmap getLoacalBitmap(String url) {
		try {
			FileInputStream fis = new FileInputStream(url);
			Bitmap bitmap = BitmapFactory.decodeStream(fis);
			return bitmap;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		}
	}

	public void setInscribeImage(String url) {
		GlideUtil.setImage(getActivity(), url, R.drawable.yspba_img_mor,
				R.drawable.yspba_img_mor, Inscribe);
	}

	public void setCompleteImage(String url, boolean bo) {
		if (bo) {
			for (int i = 0; i < item.getPointinformation().size(); i++) {
				if (i > 2) {
					item.getPointinformation().remove(i);
					i--;
				}
			}
		}

		DataKeeper.keepSerializable(getActivity(), item);
		GlideUtil.setImage(getActivity(), url, R.drawable.yspba_img_mor,
				R.drawable.yspba_img_mor, mImageView);
	}

	String[] agedata = { "当代（1949年以后）", "近代（1840~1949年之间）", "古代（1840年以前）", "不详" };
	// private void initScrollBar() {
	// // TODO Auto-generated method stub
	// ClasslistParse bean = (ClasslistParse) DataKeeper.readSerializable(
	// getActivity(), ClasslistParse.class);
	// if (bean != null) {
	// class_list = bean.getClass_list();
	// } else {
	// class_list = new ArrayList<ClasslistParse.Classlist>();
	// }
	// }

	CallbackSky typeCall = new CallbackSky() {
		@Override
		public void callBack(int index) {
			// TODO Auto-generated method stub
			option.setText(agedata[index]);
		}

	};

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.et_age:
			AlumAlert.initAgeAlertDialog(getActivity(), agedata, typeCall);
			break;
		case R.id.tv_edit:
			call.callBack(2);
			break;
		case R.id.iv_Inscribe_image:
			call.callBack(3);
			break;
		default:
			break;
		}
	}
}
